<template>
  <!-- 添加或修改项目基础信息管理对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body v-dialogDrag width="90%">
    <el-button @click="handleChose" icon="el-icon-plus" plain size="mini" type="primary"  v-if="this.proStatus==0 || this.proStatus==1">继续添加</el-button>

    <el-table :data="columns" ref="dragTable" row-key="id" v-loading="loading">
      <el-table-column align="center" label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品编码" align="center" prop="code"/>
      <el-table-column align="center" label="商品名称" prop="goodsName"/>
      <!--<el-table-column align="center" label="商品品牌" prop="brand"/>-->
      <el-table-column align="center" label="商品型号" prop="model"/>
      <el-table-column align="center" label="商品单位" prop="unit"/>
      <!--<el-table-column align="center" label="含税单价(成本)" prop="costPriceTax"/>-->
      <!--      <el-table-column label="采购数量" align="center" prop="count"/>-->
      <!--      <el-table-column label="库存数量" align="center" prop="remainderCount"/>-->
      <el-table-column align="center" label="销售单价" prop="goodsUnitPrice"/>
      <el-table-column align="center" label="订单数量" prop="counts"/>
      <el-table-column align="center" label="锁定数量" prop="goodsCount"/>
      <el-table-column align="center" label="请购数量" prop="shengyu"/>
      <el-table-column align="center" label="销售总价" prop="goodsTotalPrice"/>
      <el-table-column align="center" label="销售成本" prop="goodsBuyTotal"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作"  v-if="this.proStatus==0 || this.proStatus==1">
        <template slot-scope="scope">
          <el-popconfirm :title="`确认删除 `+scope.row.goodsName+` 数据项？`" @confirm="handleDelete(scope.row)"
                         style="margin-left: 10px;">
            <el-button icon="el-icon-delete" size="mini" slot="reference" type="text"
                       v-hasPermi="['develop:ProjectBaseInfo:remove']">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="dialog-footer" slot="footer">
      <el-button type="primary" @click="submitProject" v-if="this.proStatus==0 || this.proStatus==1">发起请购</el-button>
      <!--      <el-button type="primary" @click="submitForm">确 定</el-button>-->
      <el-button @click="cancel">取 消</el-button>
    </div>
    <goods-orderin-chose @ok="refreshPageData" ref="formChose"></goods-orderin-chose>
  </el-dialog>
</template>

<script>

    import {deleteAction, getAction, getById} from '@/api/manage'
    import GoodsOrderinChose from "./GoodsOrderinChose";

    export default {
        name: "ProjectBaseInfoFormGoodsFee",
        dicts: ['goods_customer', 'company', 'yes_no'],
        components: {
            GoodsOrderinChose
        },
        data() {
            return {
              tableMaxHeight: window.innerHeight - 400,
              // 显示搜索条件
              showSearch: true,
                // 遮罩层
                loading: true,
                activeName: "first",
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 表单参数
                form: {},
                proId: "",
                proStatus: "",
                columns: [],
                total: 0,
                // 表单校验
                rules: {
                    name: [
                        {required: true, message: "项目名称不能为空", trigger: "blur"}
                    ],
                    master: [
                        {required: true, message: "项目负责人不能为空", trigger: "blur"}
                    ],
                    customId: [
                        {required: true, message: "客户信息不能为空", trigger: "blur"}
                    ],
                },
                queryParams: {
                    pageNum: 1,
                    pageSize: 1000,
                    id: null,
                    name: null,
                    taxNo: null,
                    addr: null,
                    tel: null,
                    bankName: null,
                    bankNo: null,
                    info: null,
                    status: "0",
                    delFlag: null,
                    createBy: null,
                    createTime: null,
                    updateBy: null,
                    updateTime: null,
                    remark: null,
                    type: null,
                },
                queryParams1 :{
                  pageNum: 1,
                    pageSize: 5,
                    name: null,
                    brand: null,
                    unit: null,
                    costPrice: null,
                    count: null,
                    cost: null,
                    validityTerm: null,
                    buyId: null,
                    buyName: null,
                    buyDate: null,
                    buyType: null,
                    reimburse: null,
                    supplierId: null,
                    supplierName: null,
                    params: {
                        dataFlag: "1"
                    }
                },
                url: {
                    list: '/goodsSale/goodsSale/listSaleProject',
                    listBatch: '/goodsSale/goodsSale/listBatchSale/',
                    getById: '/projectBaseInfo/projectBaseInfo/',
                    add: '/projectBaseInfo/projectBaseInfo/add',
                    edit: '/projectBaseInfo/projectBaseInfo/edit',
                    remove: '/goodsSale/goodsSale/deleteProject/',
                    submitProject: '/projectBaseInfo/projectBaseInfo/submitProject',
                },
                ids: [],
                dataList1: [],
                columns1:[],
                total1:0,
                single: true,
                 multiple: true,
            };
        },
        created() {
          this.getList1()
        },
        methods: {
          submitProject(){
            getAction(this.url.submitProject, {id:this.proId,status:"2"}).then(response => {
              this.refreshPageData();
              this.$modal.msgSuccess("提交成功!");
              this.open = false;
              this.handleQuery();
            }).catch(() => {
            });
          },
          // 多选框选中数据
          handleSelectionChange(selection) {
                this.dataList1 = selection;
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
          getList1() {
                this.loading = true;
                getAction(this.url.list, this.queryParams1).then(response => {
                    let res = response.rows;
                    for (let i = 0; i < res.length; i++) {
                        res[i].buyDate = new Date();
                    }
                    this.columns1 = res;
                    this.total1 = response.total;
                    this.loading = false;
                });
            },
          /** 搜索按钮操作 */
          handleQuery1() {
                this.queryParams1.pageNum = 1;
                this.getList1();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.handleQuery1();
            },
            //重新计算利润,因部分旧数据未参与计算 : 商品销售金额为新增加字段
            refreshProfit() {
                let that = this;
                let params = {id: this.proId};
                getAction("/projectBaseInfo/projectBaseInfo/refreshProfit", params).then(response => {
                    that.refreshPageData();
                });
            },
            refreshPageData() {
                let that = this;
                let param = {
                    id: that.proId
                }
                that.handleUpdate(param);

            },
            /*handleChangeMoney(row) {
                let that = this;
                let handleReckonMoney = (row) => {
                    let row_money = 0;
                    row_money = that.functionNumberMul(Number(row.goodsUnitPrice), Number(row.goodsCount))
                    that.$set(row, 'goodsTotalPrice', row_money);
                };
                handleReckonMoney(row);
            },*/
            /*functionNumberMul(arg1, arg2) {
                var m = 0;
                var s1 = arg1.toString();
                var s2 = arg2.toString();
                try {
                    m += s1.split(".")[1].length;
                } catch (e) {
                }
                try {
                    m += s2.split(".")[1].length;
                } catch (e) {
                }
                return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
            },*/
            handleChose() {
                this.$refs.formChose.chose(this.proId);
            },
            getList() {
                this.loading = true;
                /*getById(this.url.listBatch, this.ids).then(response => {
                  this.columns = response.rows;
                  this.total = response.total;
                  this.loading = false;
                });*/
                this.queryParams.projectId = this.proId
                getAction(this.url.list, this.queryParams).then(response => {
                    this.columns = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            handleDelete(row) {
                const ids = row.id || this.ids;
                deleteAction(this.url.remove, ids).then(response => {
                    this.refreshPageData();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {
                });
            },
            /*handleUpdateDetail(row) {
                this.reset();
                const id = row.id || this.ids;
                getById(this.url.getById, id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "完善项目信息";
                });
            },*/
            /*updateAuditMoney(value) {
                this.$set(this.form, "auditMoney", value);
            },*/
            /*selectCustomerForPrj(value) {
                let that = this;
                that.$nextTick(() => {
                    let arr = that.$refs.myselectCustomerForPrj;
                    for (let i = 0; i < arr.length; i++) {
                        if (arr[i].currentValue == value) {
                            that.form.customName = arr[i].label;
                        }
                    }
                })
            },*/
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    delFlag: null,
                    createBy: null,
                    createTime: null,
                    updateBy: null,
                    updateTime: null,
                    remark: null,
                    name: null,
                    master: null,
                    customId: null,
                    customName: null,
                    companyName: null,
                    contractDate: null,
                    contractMoney: null,
                    auditMoney: null,
                    checkDate: null,
                    checkState: null,
                    fundCallback: null,
                    infomation: null
                };
                this.resetForm("form");
            },
            /** 新增按钮操作 */
            /*handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加项目基础信息管理";
            },*/
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids;
                this.proId = id;
                this.proStatus=row.status;
                getById(this.url.getById, id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改订单基础信息管理";
                    this.getList();
                });
            },
            /** 提交按钮 */
            /*submitForm() {
                let that = this;
                that.$refs["form"].validate(valid => {
                    if (valid) {
                        /!*const goodsSaleForm = that.form;
                        const goodsOrderIn = Object.assign({}, goodsSaleForm);
                        goodsOrderIn.columns = that.columns;
                        goodsOrderIn.form = goodsSaleForm;
                        updateGoodsSale(goodsOrderIn).then(res => {
                          that.$modal.msgSuccess(res.msg);
                          if (res.code === 200) {
                            that.$modal.msgSuccess("修改完成！");
                            that.open = false;
                            that.$emit('ok');
                          }
                        });*!/
                        if (this.form.id != null) {
                            updateAction(this.url.edit, this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                        } else {
                            addAction(this.url.add, this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                        }
                    }
                });
            },*/
        }
    };
</script>
